package com.app.utils;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.app.application.QXApplication;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import phone.qyg.com.qyg.R;

/**
 * 全局分类
 * lbh
 */
public class PopuPdfData {


	@SuppressWarnings("deprecation")
	public static PopupWindow setPupoClassifyCourse(View v,final String pdfUrl,final OnPdfState onPdfState){

		LayoutInflater inflater = (LayoutInflater) QXApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.popo_pdf_data, null);


		final PopupWindow popupWindow = new PopupWindow(view,
				ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

		popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
//		popupWindow.update();
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		

		new Thread(){
			@Override
			public void run() {
				super.run();
				try {
					showPDF(pdfUrl,onPdfState,popupWindow);
//					showPDF("http://ngzy1.wanshuyun.com/x1yw/zyjiaoan/201510/10/pc/doc/20151010-1E1994B5D305B95345CCE500EECF67EC.pdf",onPdfState,popupWindow);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();

		return popupWindow;
	}

	public static void showPDF(String urlpath,final OnPdfState onPdfState,PopupWindow popupWindow) throws Exception {
		URL u = new URL(urlpath);
		String path = createDir("test.pdf");
		byte[] buffer = new byte[1024 * 8];
		int read;
//		int ava = 0;
//		long start = System.currentTimeMillis();
		BufferedInputStream bin;
		try {
			HttpURLConnection urlcon = (HttpURLConnection) u.openConnection();
			double fileLength = (double) urlcon.getContentLength();
			bin = new BufferedInputStream(u.openStream());
			BufferedOutputStream bout = new BufferedOutputStream(
					new FileOutputStream(path));
			while ((read = bin.read(buffer)) > -1) {
				bout.write(buffer, 0, read);
//				ava += read;
//				long speed = ava / (System.currentTimeMillis() - start);
//				System.out.println("Download: " + ava + " byte(s)"
//						+ "    avg speed: " + speed + "  (kb/s)");
			}
			bout.flush();
			bout.close();
//			SystemPrintl.systemPrintl("下载完成");
//			popupWindow.dismiss();
			onPdfState.state(OnPdfState.PDFSTATE.SUCESS,path,popupWindow);
		} catch (IOException e) {
			e.printStackTrace();
//			popupWindow.dismiss();
			onPdfState.state(OnPdfState.PDFSTATE.ERROE,null,popupWindow);
		}

	}

	private static String createDir(String filename) {
		File sdcardDir = Environment.getExternalStorageDirectory();
		// 得到一个路径，内容是sdcard的文件夹路径和名字
		String path = sdcardDir.getPath() + "/qxtestDownlod";
		File path1 = new File(path);
		if (!path1.exists())
			// 若不存在，创建目录，可以在应用启动的时候创建
			path1.mkdirs();
		path = path + "/" + filename;
		return path;
	}

	public interface OnPdfState{
		public enum PDFSTATE{
			SUCESS,
			ERROE
		}

		public void state(PDFSTATE state, String url, PopupWindow popupWindow);
	}
}
